﻿using HighThroughput.Common;
using Microsoft.Win32;
using Models;
using Newtonsoft.Json;
using OSSUtility;
using Ozone.BLL;
using Ozone.Device;
using Ozone.Models;
using Rubyer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Ozone.Views.MethodCenter
{
    /// <summary>
    /// WindowMethodItem.xaml 的交互逻辑
    /// </summary>

    public partial class WindowMethodItem : Window
    {
        private bool bIsOK = false;
        public MethodItem MethodItemModelConfig = new MethodItem();
        List<string> DispensingMethodList = new List<string>();
        DispensingModel methodMain = new DispensingModel();

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="MethodItem"></param>
        public WindowMethodItem(MethodItem MethodItemConfig)
        {
            InitializeComponent();
            List<sys_dict> ListDict = Sys_DictBLL.GetDictInfoList("");
            if (ListDict != null && ListDict.Count > 0)
            {
                foreach (var item in ListDict)
                {
                    DispensingMethodList.Add(item.ItemName);
                }
            }
            ComboBox_DispensingMethod.ItemsSource = DispensingMethodList;
            txtTitle.Text = "试剂详情";

            #region
            MethodItemModelConfig.FNO = MethodItemConfig.FNO;
            MethodItemModelConfig.FStatus = MethodItemConfig.FStatus;
            #endregion
            //配液方法名称
            ComboBox_DispensingMethod.Text = MethodItemConfig.DispensingMethodName;
            //磁力加热温度
            txt_IKATemp.Text = MethodItemConfig.IKATemp.ToString();
            //磁力搅拌转速
            txt_IKASpeed.Text = MethodItemConfig.IKASpeed.ToString();
            //磁力加热时间
            txt_IKATempTime.Text = MethodItemConfig.IKATempTime.ToString();
            //磁力搅拌时间
            txt_IKASpeedTime.Text = MethodItemConfig.IKASpeedTime.ToString();
            //馏分开始试管
            //txt_Fristtesttube.Text = MethodItemConfig.Fristtesttube.ToString();
            //馏分结束试管
            txt_Endtesttube.Text = MethodItemConfig.Endtesttube.ToString();
            //馏分容量
            txt_LFCapacity.Text = MethodItemConfig.LFCapacity.ToString();
            //离心机速度
            txt_CentrifugeSpeed.Text = MethodItemConfig.CentrifugeSpeed.ToString();
            //离心时间
            txt_CentrifugeTime.Text = MethodItemConfig.CentrifugeTime.ToString();
            //离心机温度
            txt_CentrifugeTemperature.Text = MethodItemConfig.CentrifugeTemperature.ToString();



            //离心机速度
            txt_CentrifugeSpeed1.Text = MethodItemConfig.CentrifugeSpeedTwo.ToString();
            //离心时间
            txt_CentrifugeTime1.Text = MethodItemConfig.CentrifugeTimeTwo.ToString();
            //离心机温度
            txt_CentrifugeTemperature1.Text = MethodItemConfig.CentrifugeTemperatureTwo.ToString();


            txt_FlowTFC1.Text = MethodItemConfig.Flow1.ToString();


            txt_FlowTFC2.Text = MethodItemConfig.Flow2.ToString();



            //超声试管时间
            txt_ultrasoundTime.Text = MethodItemConfig.ultrasoundTime.ToString();
            //超声旋蒸瓶时间
            txt_ultrasoundXZTime.Text = MethodItemConfig.ultrasoundXZTime.ToString();
            //旋蒸温度
            txt_RotaryTemp.Text = MethodItemConfig.RotaryTemp.ToString();
            //旋蒸旋转速度
            txt_RotarySpeed.Text = MethodItemConfig.RotarySpeed.ToString();
            //旋蒸时间
            txt_RotaryTime.Text = MethodItemConfig.RotaryTime.ToString();
            if (MethodItemConfig.PumpintervalTime == null)
            {
                MethodItemConfig.PumpintervalTime = "0";
            }
            //泵间隔时间
            txt_PumpintervalTime.Text = MethodItemConfig.PumpintervalTime.ToString();

            txt_OsskjPumpfolw.Text = MethodItemConfig.OsskjPumpfolw.ToString();

            txt_BSReactorHeat1.Text = MethodItemConfig.BSReactorHeat1.ToString();


            txt_GSReactorHeat.Text = MethodItemConfig.GSReactorHeat.ToString();

            txt_HeatPumpFlow.Text = MethodItemConfig.HeatPumpFlow.ToString();

            txt_HeatPump1.Text = MethodItemConfig.HeatPump1.ToString();
            txt_HeatPump2.Text = MethodItemConfig.HeatPump2.ToString();
            txt_HeatPump3.Text = MethodItemConfig.HeatPump3.ToString();
            txt_HeatPump4.Text = MethodItemConfig.HeatPump4.ToString();

            txt_ThreeWorkBY1.Text = MethodItemConfig.ThreeWorkBY1.ToString();

            txt_ThreeWorkBY2.Text = MethodItemConfig.ThreeWorkBY2.ToString();

            txt_ThreeWorkBY3.Text = MethodItemConfig.ThreeWorkBY3.ToString();

            txt_XZOsskjPumpFlow.Text = MethodItemConfig.XZOsskjPumpFlow.ToString();

            txt_XZOsskjPumpVolume.Text = MethodItemConfig.XZOsskjPumpVolume.ToString();

        }
        public bool ShowModal()
        {
            this.ShowDialog();
            return bIsOK;
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCommOK_Click(object sender, RoutedEventArgs e)
        {
            if (!IsCanSave())
            {
                return;
            }
            //配液方法名称
            MethodItemModelConfig.DispensingMethodName = ComboBox_DispensingMethod.Text.ToString();
            //磁力加热温度
            MethodItemModelConfig.IKATemp = Utility.str2int(txt_IKATemp.Text);
            //磁力搅拌转速
            MethodItemModelConfig.IKASpeed = Utility.str2int(txt_IKASpeed.Text);
            //磁力加热时间
            MethodItemModelConfig.IKATempTime = Utility.str2int(txt_IKATempTime.Text);
            //磁力搅拌时间
            MethodItemModelConfig.IKASpeedTime = Utility.str2int(txt_IKASpeedTime.Text);
            //馏分开始试管
            MethodItemModelConfig.Fristtesttube = 1;
            //馏分结束试管
            MethodItemModelConfig.Endtesttube = Utility.str2int(txt_Endtesttube.Text);
            //馏分容量
            MethodItemModelConfig.LFCapacity = Utility.str2int(txt_LFCapacity.Text);
            //离心机速度
            MethodItemModelConfig.CentrifugeSpeed = Utility.str2int(txt_CentrifugeSpeed.Text);
            //离心时间
            MethodItemModelConfig.CentrifugeTime = Utility.str2int(txt_CentrifugeTime.Text);
            //离心机温度
            MethodItemModelConfig.CentrifugeTemperature = Utility.str2int(txt_CentrifugeTemperature.Text);



            //离心机速度
            MethodItemModelConfig.CentrifugeSpeedTwo = Utility.str2int(txt_CentrifugeSpeed1.Text);
            //离心时间
            MethodItemModelConfig.CentrifugeTimeTwo = Utility.str2int(txt_CentrifugeTime1.Text);
            //离心机温度
            MethodItemModelConfig.CentrifugeTemperatureTwo = Utility.str2int(txt_CentrifugeTemperature1.Text);



            MethodItemModelConfig.Flow1 = Utility.str2int(txt_FlowTFC1.Text);
            MethodItemModelConfig.Flow2 = Utility.str2int(txt_FlowTFC2.Text);



            //超声功率
            MethodItemModelConfig.ultrasoundPower = "";
            //超声试管时间
            MethodItemModelConfig.ultrasoundTime = Utility.str2int(txt_ultrasoundTime.Text);
            //超声旋蒸瓶时间
            MethodItemModelConfig.ultrasoundXZTime = Utility.str2int(txt_ultrasoundXZTime.Text);
            //旋蒸温度
            MethodItemModelConfig.RotaryTemp = Utility.str2int(txt_RotaryTemp.Text);
            //旋蒸旋转速度
            MethodItemModelConfig.RotarySpeed = Utility.str2int(txt_RotarySpeed.Text);
            //旋蒸压强
            MethodItemModelConfig.RotaryPa = 0;
            //旋蒸时间
            MethodItemModelConfig.RotaryTime = Utility.str2int(txt_RotaryTime.Text);



            //泵间隔时间
            MethodItemModelConfig.PumpintervalTime = txt_PumpintervalTime.Text.ToString();



            MethodItemModelConfig.OsskjPumpfolw = txt_OsskjPumpfolw.Text.ToString();
            MethodItemModelConfig.BSReactorHeat1 = txt_BSReactorHeat1.Text.ToString();
            MethodItemModelConfig.BSReactorHeat2 = "";
            MethodItemModelConfig.GSReactorHeat = txt_GSReactorHeat.Text.ToString();
            MethodItemModelConfig.HeatPumpFlow = txt_HeatPumpFlow.Text.ToString();
            MethodItemModelConfig.HeatPump1 = txt_HeatPump1.Text.ToString();
            MethodItemModelConfig.HeatPump2 = txt_HeatPump2.Text.ToString();
            MethodItemModelConfig.HeatPump3 = txt_HeatPump3.Text.ToString();
            MethodItemModelConfig.HeatPump4 = txt_HeatPump4.Text.ToString();
            MethodItemModelConfig.ThreeWorkBY1 = txt_ThreeWorkBY1.Text.ToString();
            MethodItemModelConfig.ThreeWorkBY2 = txt_ThreeWorkBY2.Text.ToString();
            MethodItemModelConfig.ThreeWorkBY3 = txt_ThreeWorkBY3.Text.ToString();
            MethodItemModelConfig.XZOsskjPumpFlow = txt_XZOsskjPumpFlow.Text.ToString();
            MethodItemModelConfig.XZOsskjPumpVolume = txt_XZOsskjPumpVolume.Text.ToString();


            bIsOK = true;
            Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCommCancel_Click(object sender, RoutedEventArgs e)
        {
            bIsOK = false;
            Close();
        }

        /// <summary>
        /// 选择配液方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBox_DispensingMethod_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            string FSolutionBottles = string.Empty;
            string FMaterialBottles = string.Empty;

            var selectedItem = ComboBox_DispensingMethod.SelectedItem;
            if (selectedItem != null)
            {
                DispensingMethod method = methodMain.ListMethod.Where(p => p.FName == selectedItem.ToString()).FirstOrDefault();
                if (method != null && method.ListMethodItem.Count > 0)
                {

                    foreach (var item in method.ListMethodItem)
                    {
                        FSolutionBottles += item.FSolutionBottlesStr;
                        FMaterialBottles += item.FMaterialBottlesStr;
                    }
                }

                FMaterialBottlesConfig.Content = "原料配置如下";
                FSolutionBottlesConfig.Content = "溶液配置如下";
                if (string.IsNullOrEmpty(FSolutionBottles))
                {
                    FSolutionBottlesStr.Text = "                              暂无配置";
                }
                else
                {
                    FSolutionBottlesStr.Text = FSolutionBottles;
                }
                if (string.IsNullOrEmpty(FMaterialBottles))
                {
                    FMaterialBottlesStr.Text = "                              暂无配置";

                }
                else
                {
                    FMaterialBottlesStr.Text = FMaterialBottles;

                }
            }
            else
            {
                FSolutionBottlesStr.Text = "";
                FMaterialBottlesStr.Text = "";

            }
        }


        /// <summary>
        /// 参数验证
        /// </summary>
        /// <returns></returns>
        private bool IsCanSave()
        {
            if (ComboBox_DispensingMethod.Text == null)
            {
                System.Windows.MessageBox.Show("请选择配液方法", "提示",
                          MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;
            }

            int Fristtesttube = 1;
            int Endtesttube = Utility.str2int(txt_Endtesttube.Text);

            if (Fristtesttube > Endtesttube)
            {
                System.Windows.MessageBox.Show("开始试管需小于结束试管", "提示",
                          MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;

            }
            else
            {
                for (int i = Fristtesttube; i <= Endtesttube; i++)
                {
                    Testtube testtube = new Testtube();
                    testtube.TesttubeNo = i;
                    testtube.Capacity = Utility.str2int(txt_LFCapacity.Text);
                    testtube.State = 0;
                    MethodItemModelConfig.ListTesttube.Add(testtube);
                }
            }


            return true;
        }
    }
}
